package Ecu911_Datos;

/**
 *
 * @author Marcelo Montaño
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;

public class ManejadorBaseDatos {
    
    private Connection connection;
    private Savepoint svpt;

    public ManejadorBaseDatos()
    {
        try {
            String url = "jdbc:postgresql://localhost/Llamadas911?user=postgres&password=marcelo";
            connection = DriverManager.getConnection(url);
            connection.setAutoCommit(false);
        } catch (SQLException e) {
                e.printStackTrace();
        }
    }

    public void commit()
    {
        try
        {
            connection.commit();
            svpt = connection.setSavepoint();

        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public Connection getConnection()
    {
        return connection;
    }

    public void openConnectionTo(String url)
    {

        try
        {			
            connection = DriverManager.getConnection(url, "Llamadas911", "marcelo");
        } catch (SQLException e)
        {
            e.printStackTrace();
        }

    }

    public void rollBack()
    {
        try
        {
            if (svpt != null)
            {
                    connection.rollback(svpt);
                    connection.commit();
            }
        } catch (SQLException e)
        {
            // TODO Errors?
            e.printStackTrace();
        }
    }
}
